﻿using BonjourQuotation.ObjectModel.AccountData;
using BonjourQuotation.ObjectModel.CustomerData;
using BonjourQuotation.UI.Models.AccountModel;
using BonjourQuotation.UI.Models.CustomerModel;
using BonjourQuotation.UI.Models.PartialModel;
using BonjourQuotation.UI.Models.ReportModel;
using BonjourQuotation.UI.Models.Shared;
using BonjourQuotation.UI.ServiceManagers;
using iTextSharp.text;
using iTextSharp.text.pdf;
using Microsoft.Reporting.WebForms;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;


namespace BonjourQuotation.UI.Controllers
{
    public class CustomerController : BaseController
    {
        CustomerManager CustomerManager;
        [Authorize]
        [HttpGet]
        public ActionResult ManageCustomers()
        {
            CustomerManager = new CustomerManager();
            var ManageCustomersData = CustomerManager.LoadCustomers(1);
            var ManageCustomersViewModel = InitManageCustomersSearchParams(ManageCustomersData);
            this.SetPermission(ManageCustomersViewModel, AllPermVModel);
            return View("ManageCustomers", ManageCustomersViewModel);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult LoadCustomersGrid(ManageCustomersGridViewModel model)
        {
            CustomerManager = new CustomerManager();
            ManageCustomersGridBData ManageCustomersData = new ManageCustomersGridBData();
            if (model.IsSearch)
            {
                ManageCustomersGridBData SearchCustomersData = new ManageCustomersGridBData();
                SearchCustomersData = InitSearchCustomer(model);
                ManageCustomersData = CustomerManager.LoadSearchedCustomers(SearchCustomersData);
            }
            else
                ManageCustomersData = CustomerManager.LoadCustomers(model.PageNum);
            var ManageCustomersViewModel = InitManageCustomersVModel(ManageCustomersData, model.PageNum);
            ManageCustomersViewModel.TopScrollPossition = model.TopScrollPossition;
            this.SetPermission(ManageCustomersViewModel, AllPermVModel);
            return PartialView("LoadCustomersGrid", ManageCustomersViewModel);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult LoadCustomer(int CustomerID)
        {
            CustomerManager = new CustomerManager();
            ManageCustomersGridBData ManageCustomersData = new ManageCustomersGridBData();
                ManageCustomersData = CustomerManager.LoadCustomerByID(CustomerID);
            var ManageCustomersViewModel = InitManageCustomersVModel(ManageCustomersData, 1);
            this.SetPermission(ManageCustomersViewModel, AllPermVModel);
            return PartialView("LoadCustomersGrid", ManageCustomersViewModel);
        }
        [Authorize]
        [HttpGet]
        public PartialViewResult LoadCustomerDetails(int CustomerID)
        {
            CustomerManager = new CustomerManager();
            var CustomerDetData = CustomerManager.LoadCustomerDetailsByID(CustomerID);
            ModelState.Clear();
            var CustomersDetViewModel = InitCustomerDetailsViewModel(CustomerDetData);
            this.SetPermission(CustomersDetViewModel, AllPermVModel);
            if (CustomerID == 0)
                CustomersDetViewModel.Active = true;
            return PartialView("LoadCustomerDetailsGrid", CustomersDetViewModel);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult LoadCustomerSearchedGroups(ManageCustomerGroupsGridViewModel model)
        {
            CustomerManager = new CustomerManager();
            CustomerGroupsBData CustomerDetData = new CustomerGroupsBData();
            if (model.SelectedGroupStatus == null)
                CustomerDetData = CustomerManager.LoadCustomerGroupsByID(model.CustomerID);
            else
                CustomerDetData = CustomerManager.LoadCustomerSearchedGroupsByID(model.CustomerID, Convert.ToInt32(model.SelectedGroupStatus));
            return PartialView("LoadCustomerGroupsGrid", InitCustomerGroupsViewModel(CustomerDetData));
        }
        [Authorize]
        [HttpGet]
        public PartialViewResult LoadCustomerGroups(int CustomerID)
        {
            CustomerManager = new CustomerManager();
            var CustomerDetData = CustomerManager.LoadCustomerGroupsByID(CustomerID);
            return PartialView("LoadCustomerGroupsGrid", InitCustomerGroupsViewModel(CustomerDetData));
        }
        [Authorize]
        [HttpGet]
        public PartialViewResult LoadCustomerQuotations(int CustomerID)
        {
            CustomerManager = new CustomerManager();
            var CustomerDetData = CustomerManager.LoadCustomerQuotationsByID(CustomerID);
            return PartialView("LoadCustomerQuotationsGrid", InitCustomerQuotationsViewModel(CustomerDetData));
        }
        [Authorize]
        [HttpGet]
        public PartialViewResult LoadCustomerInvoices(int CustomerID)
        {
            CustomerManager = new CustomerManager();
            var CustomerInvData = CustomerManager.LoadCustomerInvoicesByID(CustomerID);
            return PartialView("LoadCustomerInvoicesGrid", InitCustomerInvoicesViewModel(CustomerInvData));
        }
        [Authorize]
        [HttpGet]
        public PartialViewResult LoadCustomerPayments(int CustomerID, int? LineNo)
        {
            CustomerManager = new CustomerManager();
            var CustomerPayData = CustomerManager.LoadCustomerPaymentsByID(CustomerID);
            var CustomerPayViewModel = InitCustomerPaymentsViewModel(CustomerPayData, LineNo);
            this.SetPermission(CustomerPayViewModel, AllPermVModel);
            return PartialView("LoadCustomerPaymentsGrid", CustomerPayViewModel);
        }
        [Authorize]
        [HttpGet]
        public PartialViewResult LoadCustomerPaymentGroups(int CustomerID , int? LineNo)
        {
            CustomerManager = new CustomerManager();
            var CustomerPayGrpData = CustomerManager.LoadCustomerPaymentGroupsByID(CustomerID, LineNo);
            var CustomerPayGrpViewModel = InitCustomerPaymentGroupsViewModel(CustomerPayGrpData);
            this.SetPermission(CustomerPayGrpViewModel, AllPermVModel);
            return PartialView("LoadCustomerPaymentGroupsGrid", CustomerPayGrpViewModel);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult SaveCustomerGridData(ManageCustomersGridViewModel model)
        {
            CustomerManager = new CustomerManager();
            var CustomerData = InitManageCustomersGridBData(model);
            var EditError = CustomerManager.SaveCustomersGridData(CustomerData);
            MessageBoxViewModel MessBoxforEdit = new MessageBoxViewModel()
            {
                Message = EditError.Message != null ? "Error - " + EditError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforEdit);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult SaveCustomerDetailData(CustomerDetailsViewModel model)
        {
            CustomerManager = new CustomerManager();
            var CustomerData = InitCustomerDetailsBData(model);
            //if (ModelState.IsValid)
            //{
                if (model.CustomerID == 0)
                {
                    var CreateError = CustomerManager.CreateCustomer(CustomerData);
                    MessageBoxViewModel MessBoxforCreate = new MessageBoxViewModel()
                    {
                        Message = CreateError.Message != null ? "Error - " + CreateError.Message : "",
                    };
                    return PartialView("MessageBox", MessBoxforCreate);
                }
                var EditError = CustomerManager.SaveCustomerDetailsData(CustomerData);
                MessageBoxViewModel MessBoxforEdit = new MessageBoxViewModel()
                {
                    Message = EditError.Message != null ? "Error - " + EditError.Message : "",
                };
                return PartialView("MessageBox", MessBoxforEdit);
            //}
            //else
            //{
            //    var CustomerDetData = CustomerManager.LoadCustomerDetailsByID(model.CustomerID);
            //    return PartialView("LoadCustomerDetailsGrid", InitCustomerDetailsViewModel(CustomerDetData));
            //}
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult AddNewCustomerContactDetail(CustomerDetailsViewModel model)
        {
            CustomerManager = new CustomerManager();
            var CustomerContactDetail = InitCustomerContactAddBData(model);
            var CreateError = CustomerManager.CreateCustomerContact(CustomerContactDetail);
            MessageBoxViewModel MessBoxforCreate = new MessageBoxViewModel()
            {
                Message = CreateError.Message != null ? CreateError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforCreate);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult SaveCustomerPaymentsData(ManageCustomerPaymentsGridViewModel model)
        {
            CustomerManager = new CustomerManager();
            var CustomerPaymentsData = InitCustomerPaymentsBData(model);
            var EditError = CustomerManager.SaveCustomerPaymentsData(CustomerPaymentsData);
            if (EditError.Message != null)
            {
                MessageBoxViewModel MessBoxforEdit = new MessageBoxViewModel()
                {
                    Message = "Payment Group Type Must Be Different:",

                };
                return PartialView("MessageBox", MessBoxforEdit);
            }
            int LineNo = model.SelectedLineNo;
            var CustomerPayData = CustomerManager.LoadCustomerPaymentsByID(CustomerPaymentsData.CustomerID);
            ModelState.Clear();
            return PartialView("LoadCustomerPaymentsGrid", InitCustomerPaymentsViewModel(CustomerPayData, LineNo));
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult SaveCustomerPaymentGroupsData(ManageCustomerPaymentGroupsGridViewModel model)
        {
            CustomerManager = new CustomerManager();
            var CustomerPaymentsData = InitCustomerPaymentGroupsBData(model);
            List<int> GroupID = new List<int>();
            foreach (var Group in CustomerPaymentsData.Rows)
            {
                GroupID.Add(Group.SelectedGroupsID);
                if (GroupID.Distinct().Count() != GroupID.Count)
                {
                    MessageBoxViewModel MessBoxforDuplicate = new MessageBoxViewModel()
                    {
                        Message = "Error - Payment Groups must be different:",
                    };
                    return PartialView("MessageBox", MessBoxforDuplicate);
                }
            }

            var EditError = CustomerManager.SaveCustomerPaymentGroupsData(CustomerPaymentsData);
            if (EditError.Message != null)
            {
                MessageBoxViewModel MessBoxforEdit = new MessageBoxViewModel()
                {
                    Message = "Payment Group Type Must Be Different:",

                };
                return PartialView("MessageBox", MessBoxforEdit);
            }
            int LineNo = model.LineNo != null ? Convert.ToInt32(model.LineNo) : 0;
            var CustomerPayData = CustomerManager.LoadCustomerPaymentsByID(CustomerPaymentsData.CustomerID);
            ModelState.Clear();
            return PartialView("LoadCustomerPaymentsGrid", InitCustomerPaymentsViewModel(CustomerPayData, LineNo));
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult AddNewCustomerPayment(ManageCustomerPaymentsGridViewModel model)
        {
            CustomerManager = new CustomerManager();
            var CustomerPayment = InitCustomerPaymentAddBData(model);
            int LineNo = 0;
            var CreateError = CustomerManager.CreateCustomerPayment(CustomerPayment, ref LineNo);
            if (CreateError.Message != null)
            {
                MessageBoxViewModel MessBoxforEdit = new MessageBoxViewModel()
                {
                    Message = "Payment Group Type Must Be Different:",

                };
                return PartialView("MessageBox", MessBoxforEdit);
            }
            var CustomerPayData = CustomerManager.LoadCustomerPaymentsByID(CustomerPayment.CustomerID);
            ModelState.Clear();
            return PartialView("LoadCustomerPaymentsGrid", InitCustomerPaymentsViewModel(CustomerPayData, LineNo));
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult AddNewCustomerPaymentGroups(ManageCustomerPaymentGroupsGridViewModel model)
        {
            CustomerManager = new CustomerManager();
            var CustomerPayment = InitCustomerPaymentGroupAddBData(model);
            var CreateError = CustomerManager.CreateCustomerPaymentGroup(CustomerPayment);
            MessageBoxViewModel MessBoxforEdit;
            if (CreateError.Message != null)
            {
                MessBoxforEdit = new MessageBoxViewModel()
                {
                    Message = "Payment Group Type Must Be Different:",

                };
                return PartialView("MessageBox", MessBoxforEdit);
            }
            int LineNo = model.LineNo != null ? Convert.ToInt32(model.LineNo) : 0;
            var CustomerPayData = CustomerManager.LoadCustomerPaymentsByID(CustomerPayment.CustomerID);
            ModelState.Clear();
            return PartialView("LoadCustomerPaymentsGrid", InitCustomerPaymentsViewModel(CustomerPayData, LineNo));
        }
        [Authorize]
        [HttpPost]
        public ActionResult GetStateByCountryID(int CountryID)
        {
            CustomerManager = new CustomerManager();
            var data = CustomerManager.LoadState();
            List<System.Web.WebPages.Html.SelectListItem> StateViewModel = new List<System.Web.WebPages.Html.SelectListItem>(); ;
            foreach (var state in data)
            {
                StateViewModel.Add(new System.Web.WebPages.Html.SelectListItem() { Text = state.Value, Value = state.Key });

            }
            return Json(StateViewModel);
        }
        [Authorize]
        [HttpPost]
        public ActionResult GetCityByCountryID(int CountryID, string StateID)
        {
            CustomerManager = new CustomerManager();
            var data = CustomerManager.LoadCity(CountryID, StateID);
            List<System.Web.WebPages.Html.SelectListItem> CityViewModel = new List<System.Web.WebPages.Html.SelectListItem>(); ;
            foreach (var city in data)
            {
                CityViewModel.Add(new System.Web.WebPages.Html.SelectListItem() { Text = city.Value, Value = city.Key.ToString() });

            }
            return Json(CityViewModel);
        }
        [Authorize]
        public PartialViewResult AutoComplete(string PropName, string keyword)
        {
            QuotationManager QuotaManager = new QuotationManager();
            var data = QuotaManager.FindKeywordInEntity(PropName, keyword);
            AutoCompleteViewModel AutoCompVMode = new AutoCompleteViewModel() { Items = data };
            return PartialView("QuotaAutoComplete", AutoCompVMode);
        }
        #region private methods
        private CustomerDetailsBData InitCustomerContactAddBData(CustomerDetailsViewModel data)
        {
            CustomerDetailsBData NewCustomerContact = new CustomerDetailsBData();
            NewCustomerContact.CustomerID = data.CustomerID;
            NewCustomerContact.Name = data.Name;
            NewCustomerContact.Title = data.Title;
            NewCustomerContact.EMail = data.EMail;
            NewCustomerContact.Phone = data.Phone;
            NewCustomerContact.Fax = data.Fax;
            return NewCustomerContact;
        }
        private ManageCustomersGridBData InitManageCustomersGridBData(ManageCustomersGridViewModel data)
        {
            ManageCustomersGridBData ManCustomersBData = new ManageCustomersGridBData();
            ManCustomersBData.Rows = new List<ManageCustomersGridRowBData>();
            foreach (var Customer in data.Rows)
                ManCustomersBData.Rows.Add(new ManageCustomersGridRowBData
                {
                    CustomerID = Customer.CustomerID,
                    Active = Customer.IsActive,
                });
            return ManCustomersBData;
        }
        private CustomerPaymentsBData InitCustomerPaymentAddBData(ManageCustomerPaymentsGridViewModel data)
        {
            CustomerPaymentsBData NewCustomerPayment = new CustomerPaymentsBData();
            NewCustomerPayment.Debit = data.Debit != null ? Convert.ToDecimal(data.Debit) : (decimal?)0.00;
            NewCustomerPayment.Credit = data.Credit != null ? Convert.ToDecimal(data.Credit) : (decimal?)0.00;
            NewCustomerPayment.CustomerID = Convert.ToInt32(data.CustomerID);
            NewCustomerPayment.Description = data.Description;
            NewCustomerPayment.PaymentDate = data.PaymentDate != null ? Convert.ToDateTime(data.PaymentDate) : DateTime.Now;
            NewCustomerPayment.PayTypeID = data.AddPayTypeIDSelected;
            NewCustomerPayment.RefundF = data.RefundF;
            return NewCustomerPayment;
        }
        private CustomerPaymentGroupsBData InitCustomerPaymentGroupAddBData(ManageCustomerPaymentGroupsGridViewModel data)
        {
            CustomerPaymentGroupsBData NewCustomerPaymentGroup = new CustomerPaymentGroupsBData();
            NewCustomerPaymentGroup.Refunded = data.Refunded != null ? Convert.ToDecimal(data.Refunded) : (decimal?)0.00;
            NewCustomerPaymentGroup.Received = data.Received != null ? Convert.ToDecimal(data.Received) : (decimal?)0.00;
            NewCustomerPaymentGroup.CustomerID = Convert.ToInt32(data.CustomerID);
            NewCustomerPaymentGroup.LineNo = data.LineNo != null ? Convert.ToInt32(data.LineNo) : (int?)null;
            NewCustomerPaymentGroup.AddGroupID = data.AddGroupID;
            return NewCustomerPaymentGroup;
        }
        private CustomerPaymentsBData InitCustomerPaymentsBData(ManageCustomerPaymentsGridViewModel data)
        {
            CustomerPaymentsBData PaymentsData = new CustomerPaymentsBData();
            PaymentsData.Rows = new List<CustomerPaymentsRowBData>();
            foreach (var row in data.Rows)
            {
                PaymentsData.Rows.Add(new CustomerPaymentsRowBData()
                {
                   
                    LineNo = Convert.ToInt32(row.LineNo),
                    PayTypeID = row.PayTypeID,
                    Description = row.Description,
                    PaymentDate = row.PaymentDate != null ? Convert.ToDateTime(row.PaymentDate) : DateTime.Now,
                    RefundF = row.RefundF,
                    Debit = row.Debit != null ? Convert.ToDecimal(row.Debit) : 0,
                    Credit = row.Credit != null ? Convert.ToDecimal(row.Credit) : 0,
                });
            }
            PaymentsData.CustomerID = Convert.ToInt32(data.CustomerID);
            return PaymentsData;
        }
        private CustomerPaymentGroupsBData InitCustomerPaymentGroupsBData(ManageCustomerPaymentGroupsGridViewModel data)
        {
            CustomerPaymentGroupsBData PaymentGroupsData = new CustomerPaymentGroupsBData();
            PaymentGroupsData.Rows = new List<CustomerPaymentGroupsRowBData>();
            foreach (var row in data.Rows)
            {
                PaymentGroupsData.Rows.Add(new CustomerPaymentGroupsRowBData()
                {
                    SelectedGroupsID = row.SelectedGroupsID,
                    OldSelectedGroupsID=row.OldSelectedGroupsID,
                    Refunded = row.Refunded != null ? Convert.ToDecimal(row.Refunded) : 0,
                    Received = row.Received != null ? Convert.ToDecimal(row.Received) : 0,
                });
            }
            PaymentGroupsData.CustomerID = Convert.ToInt32(data.CustomerID);
            PaymentGroupsData.LineNo = data.LineNo != null ? Convert.ToInt32(data.LineNo) : (int?)null;
            return PaymentGroupsData;
        }
        private ManageCustomersGridViewModel InitManageCustomersVModel(ManageCustomersGridBData ManageCustomersData, int PageNum)
        {
            ManageCustomersGridViewModel ManCustomersVModel = new ManageCustomersGridViewModel();
            ManCustomersVModel.Rows = new List<ManageCustomersGridRowViewModel>();
            foreach (var Customer in ManageCustomersData.Rows)
                ManCustomersVModel.Rows.Add(new ManageCustomersGridRowViewModel
                {
                    CustomerID = Customer.CustomerID,
                    OrganizationName = Customer.OrganizationName,
                    CustomerCode = Customer.CustomerCode,
                    City = Customer.City,
                    IsActive = Customer.Active == null ? false : Convert.ToBoolean(Customer.Active),
                });
            if (ManCustomersVModel.Rows.Count == ManageCustomersGridBData.PageCount * PageNum)
                ManCustomersVModel.IsCanView = true;
            else
                ManCustomersVModel.IsCanView = false;
            return ManCustomersVModel;
        }
        private ManageCustomersGridViewModel InitManageCustomersSearchParams(ManageCustomersGridBData ManageCustomersData)
        {
            ManageCustomersGridViewModel ManCustomersVModel = new ManageCustomersGridViewModel();

            ManCustomersVModel.CitySearchText = "";
            ManCustomersVModel.CitySearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var city in ManageCustomersData.City)
                ManCustomersVModel.CitySearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = city.Value, Value = city.Value });
            ManCustomersVModel.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in ManageCustomersData.Active)
                ManCustomersVModel.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            return ManCustomersVModel;
        }
        private CustomerDetailsViewModel InitCustomerDetailsViewModel(CustomerDetailsBData data)
        {
            CustomerDetailsViewModel CustomerDetVModel = new CustomerDetailsViewModel
            {
                CustomerID = data.CustomerID,
                CustomerCode = data.CustomerCode,
                OrganizationName = data.OrganizationName,
                Address = data.Address,
                Address2 = data.Address2,
                SelectedCity = data.SelectedCity,
                CityID = data.CityID.ToString(),
                SelectedStateID = data.SelectedStateID,
                SelectedCountryID = data.SelectedCountryID.ToString(),
                PostalCode = data.PostalCode,
                CCEMail = data.CCEMail,
                PhoneNumber1 = data.PhoneNumber1,
                PhoneNumber2 = data.PhoneNumber2,
                FaxNumber = data.FaxNumber,
                Note = data.Note,
                Mail = data.Mail == null ? false : Convert.ToBoolean(data.Mail),
                Active = data.Active == null ? false : Convert.ToBoolean(data.Active),
            };
            CustomerDetVModel.ContactRows = new List<CustomerContactDetailsViewModel>();
            if (data != null && data.ContactRows != null)
                foreach (var rows in data.ContactRows)
                {
                    CustomerDetVModel.ContactRows.Add(new CustomerContactDetailsViewModel()
                    {
                        CCID = rows.CCID,
                        CustomerID = rows.CustomerID,
                        Name = rows.Name,
                        Title = rows.Title,
                        EMail = rows.EMail,
                        Phone = rows.Phone,
                        Fax = rows.Fax,
                    });
                }
            CustomerDetVModel.StateID = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var stID in data.StateID)
                CustomerDetVModel.StateID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = stID.Value, Value = stID.Key });
            CustomerDetVModel.CountryID = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var cnt in data.CountryID)
                CustomerDetVModel.CountryID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = cnt.Value, Value = cnt.Key.ToString() });
            CustomerDetVModel.City = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var city in data.City)
                CustomerDetVModel.City.Add(new System.Web.WebPages.Html.SelectListItem() { Text = city.Value, Value = city.Key.ToString() });
            return CustomerDetVModel;
        }
        private ManageCustomersGridBData InitSearchCustomer(ManageCustomersGridViewModel Data)
        {
            ManageCustomersGridBData SrchData = new ManageCustomersGridBData
            {
                OrganizationName = Data.OrganizationNameText,
                CustomerCode = Data.CustomerCodeText,
                SelectedCity = Data.CitySearchText,
                SelectedActive = Data.IsActiveSearch,
                PageNum = Data.PageNum,
            };
            return SrchData;
        }
        private CustomerDetailsBData InitCustomerDetailsBData(CustomerDetailsViewModel data)
        {
            CustomerDetailsBData CustomerBData = new CustomerDetailsBData
            {
                CustomerID = data.CustomerID,
                CustomerCode = data.CustomerCode !=null ? data.CustomerCode : "-",
                OrganizationName = data.OrganizationName != null ? data.OrganizationName : "-",
                Address = data.Address,
                Address2 = data.Address2,
                SelectedCity = data.SelectedCity,
                CityID = (data.CityID != null && data.CityID != "0") ? Convert.ToInt32(data.CityID) : (int?)null,
                SelectedStateID = data.SelectedCountryID == "1" ? (data.SelectedStateID == "0" ? null : data.SelectedStateID) : null,
                SelectedCountryID = data.SelectedCountryID != null ? Convert.ToInt32(data.SelectedCountryID) : (int?)null,
                PostalCode = data.PostalCode,
                CCEMail = data.CCEMail,
                PhoneNumber1 = data.PhoneNumber1,
                PhoneNumber2 = data.PhoneNumber2,
                FaxNumber = data.FaxNumber,
                Note = data.Note,
                Mail = data.Mail,
                Active = data.Active,
            };
            CustomerBData.ContactRows = new List<CustomerContactDetailsBData>();
            if (data.ContactRows != null)
            {
                foreach (var row in data.ContactRows)
                {
                    CustomerBData.ContactRows.Add(new CustomerContactDetailsBData()
                    {
                        CCID = row.CCID,
                        CustomerID = row.CustomerID,
                        Name = row.Name,
                        Title = row.Title,
                        EMail = row.EMail,
                        Phone = row.Phone,
                        Fax = row.Fax,
                    });
                }
            }
            return CustomerBData;
        }
        private ManageCustomerGroupsGridViewModel InitCustomerGroupsViewModel(CustomerGroupsBData data)
        {
            ManageCustomerGroupsGridViewModel InitGroups = new ManageCustomerGroupsGridViewModel();
            InitGroups.Rows = new List<CustomerGroupsViewModel>();
            foreach (var rows in data.Rows)
            {
                InitGroups.Rows.Add(new CustomerGroupsViewModel()
                {
                    GroupID = rows.GroupID,
                    ArrivalDate = rows.ArrivalDate != null ? Convert.ToDateTime(rows.ArrivalDate).ToString("dd/MMM/yy") : "",
                    DepartureDate = rows.DepartureDate != null ? Convert.ToDateTime(rows.DepartureDate).ToString("dd/MMM/yy") : "",
                    GroupCode = rows.GroupCode,
                    GroupName = rows.GroupName,
                    GroupStatusDesc = rows.GroupStatusDesc,
                    Pax = rows.Pax.ToString(),
                    Rooms = rows.Rooms.ToString(),
                    IsGir = rows.IsGir != null ?Convert.ToBoolean(rows.IsGir) : false,
                });
            }
            InitGroups.SelectedGroupStatus = "";
            InitGroups.GroupStatus = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var gstatus in data.GroupStatus)
                InitGroups.GroupStatus.Add(new System.Web.WebPages.Html.SelectListItem() { Text = gstatus.Value, Value = gstatus.Key.ToString() });
            return InitGroups;
        }
        private ManageCustomerQuotationsGridViewModel InitCustomerQuotationsViewModel(CustomerQuotationsBData data)
        {
            ManageCustomerQuotationsGridViewModel InitQuotas = new ManageCustomerQuotationsGridViewModel();
            InitQuotas.Rows = new List<CustomerQuotationsViewModel>();
            foreach (var rows in data.Rows)
            {
                InitQuotas.Rows.Add(new CustomerQuotationsViewModel()
                {
                    QuotaID = rows.QuotaID.ToString(),
                    ArrivalDate = rows.ArrivalDate != null ? Convert.ToDateTime(rows.ArrivalDate).ToString("dd/MMM/yy") : "",
                    DepartureDate = rows.DepartureDate != null ? Convert.ToDateTime(rows.DepartureDate).ToString("dd/MMM/yy") : "",
                    QuotaCode = rows.QuotaCode,
                    QuotaName = rows.QuotaName,
                    Nights = rows.Nights.ToString(),
                    Meals = rows.Meals.ToString(),
                    Accepted = rows.Accepted == null ? false : Convert.ToBoolean(rows.Accepted),
                });
            }
            return InitQuotas;
        }
        private ManageCustomerInvoicesGridViewModel InitCustomerInvoicesViewModel(CustomerInvoicesBData data)
        {
            ManageCustomerInvoicesGridViewModel InitInv = new ManageCustomerInvoicesGridViewModel();
            InitInv.Rows = new List<MangeCustomerInvoicesRowViewModel>();
            foreach (var rows in data.Rows)
            {
                InitInv.Rows.Add(new MangeCustomerInvoicesRowViewModel()
                {
                    CustomerID = rows.CustomerID.ToString(),
                    GroupID = rows.GroupID.ToString(),
                    FinalPayment = rows.FinalPayment != null ? Convert.ToDateTime(rows.FinalPayment).ToString("dd/MMM/yy") : "",
                    GroupCode = rows.GroupCode,
                    GroupName = rows.GroupName,
                    Charged = rows.Charged != null ? Convert.ToDecimal(rows.Charged).ToString("F") : "0",
                    IsGir = rows.IsGir != null ? Convert.ToBoolean(rows.IsGir) : false,
                    IsPushed = rows.IsPushed != null ? Convert.ToBoolean(rows.IsPushed) : false,
                    Payed = rows.Payed != null ? Convert.ToDecimal(rows.Payed).ToString("F") : "0",
                    Days = rows.FinalPayment != null ? (DateTime.Now.Date - Convert.ToDateTime(rows.FinalPayment).Date).TotalDays.ToString() : "0",
                    BalanceForSum = rows.Balance != null ? Convert.ToDouble(rows.Balance) : 0,
                    Balance = rows.Balance != null ? Convert.ToDouble(rows.Balance).ToString("F") : "0",
                });
            }
            InitInv.BalanceSum_0_30_ = (from invoice in InitInv.Rows 
                                        where Convert.ToDouble(invoice.Days)>0
                                        && Convert.ToDouble(invoice.Days) <= 30
                                        select Convert.ToDouble(invoice.BalanceForSum)).Sum().ToString("F");
            InitInv.BalanceSum_30_60_ = (from invoice in InitInv.Rows 
                                         where Convert.ToDouble(invoice.Days) > 30
                                         && Convert.ToDouble(invoice.Days) <= 60
                                         select Convert.ToDouble(invoice.BalanceForSum)).Sum().ToString("F");
            InitInv.BalanceSum_60_90_ = (from invoice in InitInv.Rows 
                                         where Convert.ToDouble(invoice.Days) > 60
                                         && Convert.ToDouble(invoice.Days) <= 90
                                         select Convert.ToDouble(invoice.BalanceForSum)).Sum().ToString("F");
            InitInv.BalanceSum_90_120_ = (from invoice in InitInv.Rows 
                                          where Convert.ToDouble(invoice.Days) > 90
                                          && Convert.ToDouble(invoice.Days) <= 120
                                          select Convert.ToDouble(invoice.BalanceForSum)).Sum().ToString("F");
            InitInv.BalanceSum_120_ = (from invoice in InitInv.Rows 
                                       where Convert.ToDouble(invoice.Days) > 120
                                       select Convert.ToDouble(invoice.BalanceForSum)).Sum().ToString("F");
            InitInv.BalanceSumTotal = (from invoice in InitInv.Rows
                                       select Convert.ToDouble(invoice.BalanceForSum)).Sum().ToString("F");
            return InitInv;
        }
        private ManageCustomerPaymentsGridViewModel InitCustomerPaymentsViewModel(CustomerPaymentsBData data , int? LineNo)
        {
            ManageCustomerPaymentsGridViewModel InitPay = new ManageCustomerPaymentsGridViewModel();
            InitPay.Rows = new List<CustomerPaymentsViewModel>();
            foreach (var rows in data.Rows)
            {
                InitPay.Rows.Add(new CustomerPaymentsViewModel()
                {
                    LineNo = rows.LineNo.ToString(),
                    PayTypeID = rows.PayTypeID,
                    PaymentDate = rows.PaymentDate != null ? Convert.ToDateTime(rows.PaymentDate).ToString("dd/MMM/yy") : "",
                    RefundF = Convert.ToBoolean(rows.RefundF),
                    Description = rows.Description,
                    Debit = rows.Debit != null ? Convert.ToDecimal(rows.Debit).ToString("F") : "0",
                    Credit = rows.Credit != null ? Convert.ToDecimal(rows.Credit).ToString("F") : "0",
                });
            }
            InitPay.SelectedLineNo = LineNo != null ? Convert.ToInt32(LineNo) : 1;
            InitPay.Credit = "0.00";
            InitPay.Debit = "0.00";
            InitPay.PaymentDate = DateTime.Now.ToString("dd/MMM/yy");
            InitPay.RefundF = false;
            InitPay.CustomerID = data.CustomerID.ToString();
            InitPay.PaymentTypes = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var Ptype in data.PaymentTypes)
                InitPay.PaymentTypes.Add(new System.Web.WebPages.Html.SelectListItem() { Text = Ptype.Value, Value = Ptype.Key.ToString() });
            return InitPay;
        }
        private ManageCustomerPaymentGroupsGridViewModel InitCustomerPaymentGroupsViewModel(CustomerPaymentGroupsBData data)
        {
            ManageCustomerPaymentGroupsGridViewModel InitPayGr = new ManageCustomerPaymentGroupsGridViewModel();
            InitPayGr.Rows = new List<CustomerPaymentGroupsViewModel>();
            foreach (var rows in data.Rows)
            {
                InitPayGr.Rows.Add(new CustomerPaymentGroupsViewModel()
                {
                    SelectedGroupsID = rows.SelectedGroupsID,
                    OldSelectedGroupsID = rows.OldSelectedGroupsID,
                    Refunded = rows.Refunded != null ? Convert.ToDecimal(rows.Refunded).ToString("F") : "0.00",
                    Received = rows.Received != null ? Convert.ToDecimal(rows.Received).ToString("F") : "0.00",
                });
            }
            InitPayGr.SumOfReceived = InitPayGr.Rows.Select(paygr => Convert.ToDouble(paygr.Received)).Sum().ToString("F");
            InitPayGr.SumOfRefunded = InitPayGr.Rows.Select(paygr => Convert.ToDouble(paygr.Refunded)).Sum().ToString("F");
            InitPayGr.Refunded = "0.00";
            InitPayGr.Received = "0.00";
            InitPayGr.CustomerID = data.CustomerID.ToString();
            InitPayGr.LineNo = data.LineNo.ToString();
            InitPayGr.Groups = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var Group in data.Groups)
                InitPayGr.Groups.Add(new System.Web.WebPages.Html.SelectListItem() { Text = Group.Value, Value = Group.Key.ToString() });
            return InitPayGr;
        }
        #endregion
    }
}